/* eslint-disable max-classes-per-file */

import lexicon from '@/lib/lexicon';
import { T_return } from '../base/return';
import { I_baseData, T_baseData } from './base/baseGridData';
import { I_rule, T_rule } from './base/rule';
import { T_stdReq } from './base/stdReq';
import { I_syncParams, T_ctdUpdRec, T_syncExec, T_syncParams } from './ctd';

export class T_bpaHeadDelRec extends T_syncExec {
  // filter mongo条件表达式
  filter!: object;

  rule: I_rule;

  // filter mongo条件表达式
  mto!: { rule: I_rule; dbType: string; tableName: string; filter: object }[];

  constructor() {
    super();
    // mongo条件表达式 不能为空
    this.filter = {};
    this.rule = new T_rule();
    this.mto = [];
  }
}
export interface I_bpaHeadDelRec extends T_bpaHeadDelRec {}

// bpa头插入数据记录
export class T_bpaHeadInsRec extends T_baseData {
  // private url: string = lexicon.url.bpa;
  protected befEvent: I_syncParams[];

  protected aftEvent: I_syncParams[];

  constructor() {
    super();
    this.befEvent = [];
    this.aftEvent = [];
  }

  public setBefEvent(...p: I_syncParams[]): T_bpaHeadInsRec {
    this.befEvent.push(...p);
    return this;
  }

  public setAftEvent(...p: I_syncParams[]): T_bpaHeadInsRec {
    this.aftEvent.push(...p);
    return this;
  }

  public getBefEvent(): Array<T_syncParams> {
    return this.befEvent;
  }

  public getAftEvent(): Array<T_syncParams> {
    return this.aftEvent;
  }
}
export interface I_bpaHeadInsRec extends T_bpaHeadInsRec {}

// bpa头更新数据记录
export class T_bpaHeadUpdRec extends T_baseData {
  protected befEvent: I_syncParams[];

  protected aftEvent: I_syncParams[];

  // 过滤条件 mongo bson格式
  filter: object;

  constructor() {
    super();
    this.befEvent = [];
    this.aftEvent = [];
    this.filter = {};
  }

  public setBefEvent(...p: I_syncParams[]): T_bpaHeadUpdRec {
    this.befEvent.push(...p);
    return this;
  }

  public setAftEvent(...p: I_syncParams[]): T_bpaHeadUpdRec {
    this.aftEvent.push(...p);
    return this;
  }

  public getBefEvent(): Array<T_syncParams> {
    return this.befEvent;
  }

  public getAftEvent(): Array<T_syncParams> {
    return this.aftEvent;
  }
}
export interface I_bpaHeadUpdRec extends T_bpaHeadUpdRec {}
// bpa 头
export class T_bpaHead extends T_stdReq {
  // private url: string = lexicon.url.bpa;

  insertRecords?: I_bpaHeadInsRec;

  updateRecords?: I_bpaHeadUpdRec;

  removeRecords?: I_bpaHeadDelRec;

  constructor() {
    super();
    this.insertRecords = new T_bpaHeadInsRec();
    this.updateRecords = new T_bpaHeadUpdRec();
    this.removeRecords = new T_bpaHeadDelRec();
  }

  public setInsertRec(p: I_bpaHeadInsRec): T_bpaHead {
    this.insertRecords = p;
    return this;
  }

  public setUpdateRec(p: I_bpaHeadUpdRec): T_bpaHead {
    this.updateRecords = p;
    return this;
  }

  public setRemoveRec(p: I_bpaHeadDelRec): T_bpaHead {
    this.removeRecords = p;
    return this;
  }
}
export interface I_bpaHead extends T_bpaHead {}

// 单身插入
export class T_bpaBodyInsRec extends T_syncExec {
  // 排序时，给定的范围过滤条件条件bson格式
  // 当basepanel 单身新增时：是单身m2o 主单id的单身字段与值。
  // 当basepanel 新增单据时，此处是单身的对应字段
  scope: object;

  data: I_baseData[];

  constructor() {
    super();
    this.data = [];
    this.scope = {};
  }

  public setScope(p: object): T_bpaBodyInsRec {
    this.scope = p;
    return this;
  }

  public setData(...d: T_baseData[]): T_bpaBodyInsRec {
    this.data.push(...d);
    return this;
  }
}
export interface I_bpaBodyInsRec extends T_bpaBodyInsRec {}

// bpa单身更新记录
export class T_bpaBodyUpdRec extends T_ctdUpdRec {}
export interface I_bpaBodyUpdRec extends T_bpaBodyUpdRec {}

// 单身删除
export class T_bpaBodyDelRec extends T_syncExec {
  // private url: string = lexicon.url.bpa;

  rule: I_rule;

  filter: object;

  constructor() {
    super();
    this.rule = {} as I_rule;
  }
}
export interface I_bpaBodyDelRec extends T_bpaBodyDelRec {}

// bpa API Body 单身类
export class T_bpaBody {
  // private url: string = lexicon.url.bpa;

  rule: I_rule;

  data!: {
    tableName: string;
    dbType: string;
    insertRecords?: I_bpaBodyInsRec;
    updateRecords?: I_bpaBodyUpdRec;
    removeRecords?: I_bpaBodyDelRec;
  }[];

  constructor() {
    this.data = [];
  }
}
// bpa API Body 结构体接口
export interface I_bpaBody extends T_bpaBody {}

// bpa API 参数接口类
export class T_bpaArg {
  private url: string = lexicon.url.bpa;

  protected cancel?: any;

  head: I_bpaHead;

  body: I_bpaBody;

  constructor() {
    this.head = new T_bpaHead();
    this.body = new T_bpaBody();
  }

  public setHead(p: I_bpaHead): T_bpaArg {
    this.head = p;
    return this;
  }

  public setBody(p: I_bpaBody): T_bpaArg {
    this.body = p;
    return this;
  }

  public setCancel(p: any): T_bpaArg {
    this.cancel = p;
    return this;
  }

  /**
   * @description: 输出Json对象
   * @return {T_error} 如果不出错在T_error中通过getReturnData('ctdJson')得到数据
   * @author: wiz
   * @Date: 2022-12-01 14:24:28
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */
  public outJson(): T_return {
    const err = new T_return();
    const obj: any = {};
    let i = 0;
    if (Object.keys(this.head).length !== 0) {
      i++;
      obj['head'] = this.head;
    }
    if (Object.keys(this.body).length !== 0) {
      i++;
      obj['body'] = this.body;
    }
    if (i === 0) {
      err.errmsg = `-119,FE,80`; // 数据为空
    }
    err.setReturnData('bpaJoson', obj);
    return err;
  }
  // private chkIsEmpty(obj:object):boolean{

  // }
}
// bpa API 参数接口
export interface I_bpaArg extends T_bpaArg {}

// bpa API 新增参数结构接口
export class T_addbpaArg {
  private url: string = lexicon.url.bpa;

  cancel?: any;

  head?: {
    funName: string;
    dbType: string;
    rule: I_rule;
    insertRecords: I_bpaHeadInsRec;
  };

  body?: {
    bodyRule: I_rule;

    data: {
      tableName: string;
      dbType: string;
      insertRecords: I_bpaBodyInsRec;
    }[];
  };

  constructor() {
    this.head = {
      funName: '',
      dbType: '',
      insertRecords: new T_bpaHeadInsRec(),
      rule: new T_rule()
    };
    this.body = {
      bodyRule: new T_rule(),
      data: []
    };
  }

  public setHeadFunName(p: string): T_addbpaArg {
    this.head.funName = p;
    return this;
  }

  public setHeadDbType(p: string): T_addbpaArg {
    this.head.dbType = p;
    return this;
  }

  public setHeadRule(p: I_rule): T_addbpaArg {
    this.head.rule = p;
    return this;
  }

  public setBodyRule(p: I_rule): T_addbpaArg {
    this.body.bodyRule = p;
    return this;
  }

  public setHeadInsRec(p: I_bpaHeadInsRec): T_addbpaArg {
    this.head.insertRecords = p;
    return this;
  }

  public addBodyData(
    tableName: string,
    dbType: string,
    insertRecords: I_bpaBodyInsRec
  ): T_addbpaArg {
    this.body.data.push({ tableName, dbType, insertRecords });
    return this;
  }
}
export interface I_addbpaArg extends T_addbpaArg {}
// bpa API 更新参数结构接口
export class T_updbpaArg {
  private url: string = lexicon.url.bpa;

  cancel?: any;

  head?: {
    funName: string;
    dbType: string;
    updateRecords: I_bpaHeadUpdRec;
    rule: I_rule;
  };

  body?: {
    bodyRule: I_rule;
    data?: {
      tableName: string;
      dbType: string;
      updateRecords: I_bpaBodyUpdRec;
    }[];
  };

  constructor() {
    this.head = {
      funName: '',
      dbType: '',
      updateRecords: new T_bpaHeadUpdRec(),
      rule: new T_rule()
    };
    this.body = {
      bodyRule: new T_rule(),
      data: []
    };
  }
}
export interface I_updbpaArg extends T_updbpaArg {}
// bpa API 删除参数结构接口
export class T_delbpaArg {
  private url: string = lexicon.url.bpa;

  cancel?: any;

  // 单头
  head?: {
    funName: string;
    dbType: string;
    removeRecords: I_bpaHeadDelRec;
    rule: I_rule;
  };

  // 单身 单头有数据单身无效
  body?: {
    bodyRule: I_rule;
    data: {
      tableName: string;
      dbType: string;
      removeRecords: I_bpaBodyDelRec;
    }[];
  };

  constructor() {
    this.head = {
      funName: '',
      dbType: '',
      removeRecords: new T_bpaHeadDelRec(),
      rule: new T_rule()
    };
    this.body = {
      bodyRule: new T_rule(),
      data: []
    };
  }
}
export interface I_delbpaArg extends T_delbpaArg {}
